﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Google.GData.Client;
using Google.GData.Photos;
using System.IO;
using Interface;

namespace GooglePicasaPlugin
{
    public partial class GPPControl : UserControl
    {
        IPluginHost host;
        IPLogger logger;
        List<Image> Images;

        protected int NUMBER_OF_PHOTOS_TO_RETRIEVE = 20;
        protected PicasaService pservice;

        public GPPControl(IPluginHost _host)
        {
            InitializeComponent();
            host = _host;
            logger = host.Logger;
            Images = new List<Image>();
        }

        private void search_Click(object sender, EventArgs e)
        {
            Images.Clear();
            pservice = new PicasaService("mogco");
            PhotoQuery query = new PhotoQuery("https://picasaweb.google.com/data/feed/api/all");
            query.Query = textBox1.Text;
            query.NumberToRetrieve = NUMBER_OF_PHOTOS_TO_RETRIEVE;

            PicasaFeed feed = pservice.Query(query);
            Bitmap temp;
            int i = 0;
            try
            {
                foreach (PicasaEntry entry in feed.Entries)
                {
                    if (entry.IsPhoto)
                    {
                        Stream stream = pservice.Query(new Uri(entry.Content.AbsoluteUri));
                        temp = new Bitmap(stream);
                        Images.Add(temp);
                    }
                    i++;
                }
            }
            catch(Exception ex)
            {
                logger.PWriteMessage("Error downloading and save picture." + ex.ToString());
            }
            logger.PWriteMessage("Yeah!");
            //host.BaseForm.SetImages(Images);
        }
    }
}
